package com.bolt.admin.module.sys.service;


import com.bolt.admin.module.sys.entity.TableHeadConfig;
import com.bolt.admin.module.sys.repository.TableHeadConfigRepository;
import com.bolt.support.base.service.BaseService;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author 米超
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class TableHeadConfigService extends BaseService <TableHeadConfig, String> {

    @Autowired
    private TableHeadConfigRepository tableHeadConfigRepository;


    @Override
    protected BasicJpaRepository <TableHeadConfig, String> getRepository() {
        return tableHeadConfigRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "tableConfigCache", allEntries = true)
    @Override
    public TableHeadConfig save(TableHeadConfig entity) {
        return super.save(entity);
    }

    /**
     * 　序号重排
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void reorder(String... ids) {
        int orderRank = 0;
        for (String id : ids) {
            Optional <TableHeadConfig> head = findOne(id);
            if (head.isPresent()) {
                head.get().setOrderRank(orderRank);
                orderRank++;
            }
        }
    }
}
